#ifndef GENETIC_ALGORITHM_H
#define GENETIC_ALGORITHM_H

#include <QVector>
#include <vector>

struct Individual {
    std::vector<int> path;
    double fitness;
};

class GeneticAlgorithm {
public:
    GeneticAlgorithm(const QVector<QVector<double>> &distanceMatrix, int populationSize = 100, int generations = 1000, double mutationRate = 0.05);

    Individual solve();

    // 设置和获取参数的成员函数
    void setPopulationSize(int size);
    int getPopulationSize() const;

    void setGenerations(int gens);
    int getGenerations() const;

    void setMutationRate(double rate);
    double getMutationRate() const;

private:
    void evaluateFitness(Individual &individual);
    Individual createRandomIndividual();
    void mutate(Individual &individual);
    Individual crossover(const Individual &parent1, const Individual &parent2);
    Individual rouletteWheelSelection(const std::vector<Individual> &population); // 新增的选择函数

    QVector<QVector<double>> distanceMatrix;
    int populationSize;
    int generations;
    double mutationRate;
    int numberOfCities;
};

#endif // GENETIC_ALGORITHM_H
